import 'dart:math';
import 'package:common/export/tools.dart';
import 'package:components/lara_press_shimmer.dart';
import 'package:flutter/material.dart';

class LaraPressDetailLoading extends StatelessWidget {
  const LaraPressDetailLoading({super.key});

  @override
  Widget build(BuildContext context) {
    final screenHeight = MediaQuery.of(context).size.height;
    final screenWidth = MediaQuery.of(context).size.width;

    // ===== 上面固定区域大概占用的高度 =====
    final usedHeight = 20.h + 10.h + 32.h + 10.h + 14.h + 2.h + 8.h + 10.h;

    // ===== 每行 shimmer 高度 + 行间距 =====
    final lineHeight = 10.h + 4.h;

    // ===== 能放多少行，向下取整 =====
    final remainingLines = max(
      1,
      ((screenHeight - usedHeight) / lineHeight).floor(),
    );

    return ListView(
      padding: EdgeInsets.zero,
      shrinkWrap: true,
      physics: const NeverScrollableScrollPhysics(),
      children: [
        // 标题大块
        LaraPressShimmer(height: 20.h, width: double.infinity),
        SizedBox(height: 10.h),
        // 头像 + 用户信息
        Row(
          spacing: 10.w,
          children: [
            LaraPressShimmer(height: 32.h, width: 32.h, radius: 32.h),
            Column(
              spacing: 2.h,
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                LaraPressShimmer(height: 14.h, width: double.infinity),
                LaraPressShimmer(height: 8.h, width: 200.w),
              ],
            ).expand(),
          ],
        ),
        SizedBox(height: 10.h),

        // 剩余部分自动填充
        Column(
          spacing: 8.h,
          crossAxisAlignment: CrossAxisAlignment.start,
          children: List.generate(remainingLines, (index) {
            final rand = Random(index);

            // 每 6~8 行插入一张“图片加载器”
            if (index % 7 == 3) {
              return LaraPressShimmer(
                height: 200.h,
                width: double.infinity,
                radius: 8.r,
              );
            }
            // 正文段落（宽度随机 0.5 ~ 1）
            final factor = 0.5 + rand.nextDouble() * 0.5;
            final width = screenWidth * factor;
            return LaraPressShimmer(height: 10.h, width: width);
          }),
        ),
      ],
    );
  }
}
